package eteam.aps.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import eteam.aps.api.U8Api;
import eteam.aps.common.R;
import eteam.aps.common.exceptions.BusinessException;
import eteam.aps.common.helper.Helper;
import eteam.aps.model.dto.ProductConsumerDto;
import eteam.aps.model.entity.Produce;
import eteam.aps.model.entity.ProduceInv;
import eteam.aps.model.entity.other.OutBound;
import eteam.aps.service.ProductConsumerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 生产领料控制层
 *
 * @author LiuLei
 */
@Api(tags = "生产领料")
@RestController
@RequestMapping("/pcc")
public class ProductConsumeController {

    @Autowired
    private ProductConsumerService productConsumerService;

    @PostMapping("/goo")
    @ApiOperation("获取其他出库单") // 任务令条码
    @ApiImplicitParam(name = "scan", value = "出库单单号")
    public R<List<OutBound>> getOtherOutbound(String scan) throws Exception {
        if (Helper.isEmpty(scan))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "出库单单号不能为空！");
        List<OutBound> outbound = productConsumerService.getOtherOutbound(scan);
        long count = outbound.stream().map(OutBound::getInvCode).distinct().count();
        if (count != outbound.size())
            throw new BusinessException(HttpStatus.BAD_REQUEST, "单号异常！");
        return R.success(outbound);
    }

    @PostMapping("/gpi")
    @ApiOperation("获取生产领料单出库信息") // 任务令条码
    @ApiImplicitParam(name = "scan", value = "生产订单条码")
    public R<List<ProduceInv>> getProductInv(String scan) throws Exception {
        if (Helper.isEmpty(scan))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "生产订单条码不能为空！");
        return R.success(U8Api.getProduceInv(scan));
    }

    @PostMapping("/gav")
    @ApiOperation("查询领料库存信息【主键规则：物料编号】")
    @ApiImplicitParam(name = "scan", value = "领料单条码")
    public R<Produce> getProducer(String scan) {
        if (Helper.isEmpty(scan))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "领料单条码不能为空！");
        return R.success(U8Api.getProduce(scan));
    }

    @PostMapping("/gsb")
    @ApiOperation("查询领料库存信息")
    @ApiImplicitParam(name = "barCode", value = "领料单条码")
    public R<Map<?, ?>> getStockByBarCode(String barCode) throws JsonProcessingException {
        if (Helper.isEmpty(barCode))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "领料单条码不能为空！");
        return R.success(productConsumerService.getStockByBarCode(barCode));
    }

    @PostMapping("/gcs")
    @ApiOperation("根据条件获取当前库存量")
    @ApiImplicitParam(name = "ProductConsumerDto", value = "获取库存量")
    public R<BigDecimal> getCurrentStock(@RequestBody ProductConsumerDto productConsumerDto) throws Exception {
        // 取消批号判断
//        if (Helper.isEmpty(productConsumerDto.getBatch()))
//            throw new BusinessException(HttpStatus.BAD_REQUEST, "批号不能为空！");
        if (Helper.isEmpty(productConsumerDto.getInvCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "物料编号不能为空！");
        if (Helper.isEmpty(productConsumerDto.getWhCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "仓库编号不能为空！");
        BigDecimal currentStock = productConsumerService.getCurrentStock(productConsumerDto);
        if (currentStock == null || currentStock.compareTo(BigDecimal.ZERO) == 0)
            throw new BusinessException(HttpStatus.BAD_REQUEST, "暂无此库存量数据！");
        return R.success(currentStock);
    }

    @PostMapping("/aaob")
    @ApiOperation("提交一张出库单")
    @ApiImplicitParam(name = "map", value = "出库单信息")
    public R<String> addOutBound(@RequestBody Map<String, Object> map) throws Exception {
        if (map.isEmpty()) throw new BusinessException(HttpStatus.BAD_REQUEST, "参数非法异常！");
        return R.success(productConsumerService.addBound(map));
    }
}
